package cn.com.ddapp.ddm;

import android.content.Context;
import android.os.Environment;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;

/**
 * 应用程序配置类：用于保存用户相关信息及设置
 * 
 * @author FireAnt（http://my.oschina.net/LittleDY）
 * @created 2014年9月25日 下午5:29:00
 * 
 */
public class AppConfig {

	private final static String APP_CONFIG = "config";

	public final static String CONF_COOKIE = "cookie";

	public final static String CONF_APP_UNIQUEID = "APP_UNIQUEID";

	public static final String KEY_LOAD_IMAGE = "KEY_LOAD_IMAGE";
	public static final String KEY_NOTIFICATION_ACCEPT = "KEY_NOTIFICATION_ACCEPT";
	public static final String KEY_NOTIFICATION_SOUND = "KEY_NOTIFICATION_SOUND";
	public static final String KEY_NOTIFICATION_VIBRATION = "KEY_NOTIFICATION_VIBRATION";
	public static final String KEY_NOTIFICATION_DISABLE_WHEN_EXIT = "KEY_NOTIFICATION_DISABLE_WHEN_EXIT";
	public static final String KEY_CHECK_UPDATE = "KEY_CHECK_UPDATE";
	public static final String KEY_DOUBLE_CLICK_EXIT = "KEY_DOUBLE_CLICK_EXIT";

	public static final String LAST_QUESTION_CATEGORY_IDX = "LAST_QUESTION_CATEGORY_IDX";
	public static final String KEY_DAILY_ENGLISH = "KEY_DAILY_ENGLISH";
	public static final String KEY_GET_LAST_DAILY_ENG = "KEY_GET_LAST_DAILY_ENG";
	public static final String KEY_TWEET_DRAFT = "KEY_TWEET_DRAFT";
	public static final String KEY_NOTE_DRAFT = "KEY_NOTE_DRAFT";
	public static final String KEY_QUESTION_TITLE_DRAFT = "KEY_QUESTION_TITLE_DRAFT";
	public static final String KEY_QUESTION_CONTENT_DRAFT = "KEY_QUESTION_CONTENT_DRAFT";
	public static final String KEY_QUESTION_TYPE_DRAFT = "KEY_QUESTION_TYPE_DRAFT";
	public static final String KEY_QUESTION_LMK_DRAFT = "KEY_QUESTION_LMK_DRAFT";

	public static final String KEY_FRITST_START = "KEY_FRIST_START";

	public static final String APP_QQ_KEY = "100942993";
	public static final String RESULT_OK = "OK";
	public static final String RESULT_KEY = "result";
//	public static final Integer user_id = 7;
	public static  final String MONEY_SYMBEL = "￥";
	public static final String SHOPCAR_NUM  = "shopcar_num";

	public static  int CITY_ID = 182;
	public static  double LNG = 113.3612;
	public static  double LAT = 23.12468;
    public static  int SOID = 1;
    public static  int Goods_ID = 1;
    public static  String AddrId_ID = "";
    public static  int TEMP = 1;
    public static  int CARTTEMP = 0;
    public static  String GoodsId  = "98";
    public static  String ID_ONE = "17";
    public static  String ID_TWO= "result";
    public static  String ID_THREE = "result";
    public static  String ID_FOUR = "result";
    public static  String ID_FIVE= "result";
    public static  String SP_NAME= "result";
    public static  String City_NAME= "丰城";
    public static  String IMAGE_URL= "";
    public static  String GOOD_VIDEO= "";
    public static  String GOOD_VIDEOIMG= "";
    public static  String RESULTONE = "已结束";
    public static  String RESULTTWO = "已结束";
    public static  String RESULTTHREE = "已结束";
    public static  String RESULTFOUR = "已结束";
    public static  String RESULTFIVE= "已结束";
    public static  String FVISIT= "1";//第一次访问
    public static  String APPFVISIT= "1";//APP第一次访问
    public static  String ATMFVISIT= "1";//ATM第一次访问
    public static  String FIN= "1";
    public static  String TOMAIN= "2";
//    public static String Content= "hhhhhh";
    public static  double SP_PRICE= 1;
    public static  double SP_PRICE1= 1;
    public static  int WID = 3;



	//设置
	public static final String MODEL_CHOOSE = "model_choose";//模式选择
	public static final String WORD_SIZE = "word_size";//字体大小
	public static final String IMG_SHOW = "img_show";//图片显示
	public static final String NEWS_PUSH = "news_push";//消息推送

	public static final double ORDER_DEAD_LINE = 5000.00;

	public static final String SPLIT = "@@";
	public static final String IMGS = "imgs_";


	public static boolean isRefreshShocpar = false;

	public static final String SECRETKEY = "USN4PP6JU9A8S8VB";
//    public static final int MAIN_TAB_2 = 0x02;

	public static final int MAIN_TAB_2 = 0x02;
	public static final int MAIN_TAB_3 = 0x03;
	public static final int MAIN_TAB_4 = 0x04;
	public static final int LOGOUT = 0x05;
    public static final int MAIN_TAB_5 = 0x05;

	public static final String unAuth = "未审核";
	public static final String authIng = "审核中";
	public static final String authFail= "审核不通过";
	public static final String authSuccess = "审核通过";

	// 默认存放图片的路径
	public final static String DEFAULT_SAVE_IMAGE_PATH = Environment
			.getExternalStorageDirectory()
			+ File.separator
			+ "SOMOTELD"
			+ File.separator + "ddapp_img" + File.separator;

	// 默认存放文件下载的路径
	public final static String DEFAULT_SAVE_FILE_PATH = Environment
			.getExternalStorageDirectory()
			+ File.separator
			+ "SOMOTELD"
			+ File.separator + "download" + File.separator;


	private Context mContext;
	private static AppConfig appConfig;

	public static AppConfig getAppConfig(Context context) {
		if (appConfig == null) {
			appConfig = new AppConfig();
			appConfig.mContext = context;
		}
		return appConfig;
	}


	public String get(String key) {
		Properties props = get();
		return (props != null) ? props.getProperty(key) : null;
	}

	public Properties get() {
		FileInputStream fis = null;
		Properties props = new Properties();
		try {
			// 读取files目录下的config
			// fis = activity.openFileInput(APP_CONFIG);

			// 读取app_config目录下的config
			File dirConf = mContext.getDir(APP_CONFIG, Context.MODE_PRIVATE);
			fis = new FileInputStream(dirConf.getPath() + File.separator
					+ APP_CONFIG);

			props.load(fis);
		} catch (Exception e) {
		} finally {
			try {
				fis.close();
			} catch (Exception e) {
			}
		}
		return props;
	}

	private void setProps(Properties p) {
		FileOutputStream fos = null;
		try {
			// 把config建在files目录下
			// fos = activity.openFileOutput(APP_CONFIG, Context.MODE_PRIVATE);

			// 把config建在(自定义)app_config的目录下
			File dirConf = mContext.getDir(APP_CONFIG, Context.MODE_PRIVATE);
			File conf = new File(dirConf, APP_CONFIG);
			fos = new FileOutputStream(conf);

			p.store(fos, null);
			fos.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				fos.close();
			} catch (Exception e) {
			}
		}
	}

	public void set(Properties ps) {
		Properties props = get();
		props.putAll(ps);
		setProps(props);
	}

	public void set(String key, String value) {
		Properties props = get();
		props.setProperty(key, value);
		setProps(props);
	}

	public void remove(String... key) {
		Properties props = get();
		for (String k : key)
			props.remove(k);
		setProps(props);
	}
}
